import sys
import json

sys.path.append(r'./')

from codesecurity.feature.objects import Ast,AstNode,CommonFeatureSet
from codesecurity.feature.fusion_ast import *
from codesecurity.feature.property_graph import *

from collections import Counter

def printlist(list):
    for i in list:
        print(i)

obfuscate_levels=['light','medium','heavy']

choose_file='bad/127d09ef46a3e8c56e02812c630ab3ab_invoice_scan_fQaneK.js'

obfuscate_file=f'/home/passwd123/XiaoweiGuo/vscode/code-security/data/js/obfucscate/{{}}/jstap_train/{choose_file}'

test_files=[f'/home/passwd123/XiaoweiGuo/vscode/code-security/data/js/jstap_train/{choose_file}']
test_files+=[obfuscate_file.format(e) for e in obfuscate_levels]

save_files='temp.json'

result={}
counter=Counter()
for i,test_file in enumerate(test_files):
    features=CommonFeatureSet.from_file(test_file)
    counter.update(features.tokens)
    
    result[i]=counter.most_common(1000)
    
with open(save_files,'w') as f:
    json.dump(result,f,ensure_ascii=False)
# builder=FusionAstBuilder(get_anchor_node_vision_js())
# fusion_nodes,fusion_mapping=builder.get_merge_state(features)

# for node in fusion_nodes:
#     print(node.node_value,len(node.node_indexs))
#     printlist([(features.nodes[index].ast_type,features.nodes[index].ast_value) for index in node.node_indexs])
#     #break
#printlist([(node.node_type,node.node_value) for node in fusion_nodes])